package com.hourses.controller;

import com.hourses.page.Page;
import com.hourses.pojo.CurrentUser;
import com.hourses.pojo.InStore;
import com.hourses.pojo.Purchase;
import com.hourses.pojo.Result;
import com.hourses.service.impl.PurchaseServiceImpl;
import com.hourses.service.impl.StoreServiceImpl;
import com.hourses.utils.TokenUtils;
import com.hourses.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/8 12:53
 */

//采购管理模块

/**
 * 采购流程：先产生采购单，确认采购数量，采购人以及联系电话以后，生成采购单，采购完成以后确认采购无误以后，之后进行入库操作，
 * 入库前检查信息无误以后，再进行真正的入库
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {
    //产生采购单    http://localhost:3000/api/purchase/purchase-add  Post
    //具体操作就是向buy_list表中插入数据
    @Autowired
    private PurchaseServiceImpl purchaseService;
    @PostMapping("/purchase-add")
    public Result addBuyList(@RequestBody Purchase purchase){
        Result result = purchaseService.addBuyList(purchase);
        return result;
    }

    //查询仓库列表   http://localhost:3000/api/purchase/store-list
    @Autowired
    private StoreServiceImpl storeService;
    @GetMapping("/store-list")
    public Result selectAllStore(){
        Result result = storeService.selectAllStores();
        return result;
    }

    //展示采购列表   http://localhost:3000/api/purchase/purchase-page-list   GET
    //             buyList对象： storeId
    //                          startTime   追加
    //                          endTime     追加
    //                          productName 追加
    //                          buyUser
    //                          isIn
    //             page对象：
    //                          pageSize
    //                          pageNum
    //                          totalNum
    //             前端展示的数据：
    //                          storeName       store表
    //                          productName     product表
    //                          buy_num         buy_list表
    //                          fact_buy_num    buy_list表
    //                          buy_user        buyList表
    //                          now()   采购时间
    //                          phone           buyList表

    @GetMapping("/purchase-page-list")
    public Result selectBuyList(Purchase purchase, Page page){
        Result result = purchaseService.selectBuyList(purchase, page);
        return result;
    }

    //删除采购单   http://localhost:3000/api/purchase/purchase-delete/48
    @RequestMapping("/purchase-delete/{buyId}")
    public Result deleteBuyList(@PathVariable Integer buyId){
        Result result = purchaseService.deleteBuyList(buyId);
        return result;
    }

    //修改采购单   http://localhost:3000/api/purchase/purchase-update
    @RequestMapping("/purchase-update")
    public Result updateBuyList(@RequestBody Purchase purchase){
        Result result = purchaseService.updateBuyList(purchase);
        return result;
    }


    //生成入库单    http://localhost:3000/api/purchase/in-warehouse-record-add    POST
    //有两步操作：
    //step1:修改buyList表中的is_in字段为1
    //step2:向in_store表中添加数据
    @Autowired
    private TokenUtils tokenUtils;
    @PostMapping("/in-warehouse-record-add")
    public Result createInStoreData(@RequestBody InStore inStore, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME)String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        inStore.setCreateBy(currentUser.getUserId());
        inStore.setInNum(inStore.getFactBuyNum());
        Result result = purchaseService.createInStoreData(inStore);
        return result;
    }
}
